package zhihui.mobile.ui.shop.sales;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zgm.utils.DateUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.bean.shop.sales.SalesListData;
import zhihui.mobile.bean.shop.sales.SalesShopInfo;
import zhihui.mobile.config.Config;

/**
 * Created by 张公铭 on 2017/8/2 0002.
 */

public class SalesAdapter extends BaseAdapter
{
    private SalesListData curSalesInfo = new SalesListData();
    private List<SalesShopInfo> mlist = new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;
    private IButtonBuyListener listener;

    public interface IButtonBuyListener
    {
        void buy(SalesShopInfo shopIno, int salesId);
    }

    public SalesAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setMlist(List<SalesShopInfo> list)
    {
        if (mlist == null)
        {
            mlist = new ArrayList<>();
        }
        mlist.clear();
        mlist.addAll(list);
    }

    public void setCurSalesInfo(SalesListData curSalesInfo)
    {
        this.curSalesInfo = curSalesInfo;
    }

    public SalesListData getCurSalesInfo()
    {
        return curSalesInfo;
    }

    public void setListener(IButtonBuyListener listener)
    {
        this.listener = listener;
    }

    @Override
    public int getCount()
    {
        return mlist.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mlist.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ImageView iv_icon;
        TextView tv_name, tv_desc, tv_price, tv_original_price, tv_sell;
        ProgressBar pb_progress;
        Button btn_buy;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.shop_sales_item, null);
            iv_icon = (ImageView) convertView.findViewById(R.id.iv_sales_item_icon);
            tv_name = (TextView) convertView.findViewById(R.id.tv_sales_item_name);
            tv_desc = (TextView) convertView.findViewById(R.id.tv_sales_item_desc);
            tv_price = (TextView) convertView.findViewById(R.id.tv_sales_item_price);
            tv_original_price = (TextView) convertView.findViewById(R.id.tv_sales_item_original_price);
            pb_progress = (ProgressBar) convertView.findViewById(R.id.pb_sales_item_progress);
            tv_sell = (TextView) convertView.findViewById(R.id.tv_sales_item_sell_desc);
            btn_buy = (Button) convertView.findViewById(R.id.btn_sales_item_buy);
            ViewCache cache = new ViewCache();
            cache.iv_icon = iv_icon;
            cache.tv_name = tv_name;
            cache.tv_desc = tv_desc;
            cache.tv_price = tv_price;
            cache.tv_sell = tv_sell;
            cache.tv_original_price = tv_original_price;
            cache.pb_progress = pb_progress;
            cache.btn_buy = btn_buy;

            convertView.setTag(cache);
        } else
        {
            ViewCache cache = (ViewCache) convertView.getTag();
            iv_icon = cache.iv_icon;
            tv_name = cache.tv_name;
            tv_desc = cache.tv_desc;
            tv_price = cache.tv_price;
            tv_sell = cache.tv_sell;
            tv_original_price = cache.tv_original_price;
            pb_progress = cache.pb_progress;
            btn_buy = cache.btn_buy;
        }

        final SalesShopInfo shopInfo = mlist.get(position);
        ImageLoader.getInstance().displayImage(Config.FILE_URL + shopInfo.getThumbnail(), iv_icon);
        tv_name.setText(shopInfo.getTitle());
        tv_price.setText("￥" + ((double) shopInfo.getAmount() / 100));
        tv_original_price.setText("￥" + ((double) shopInfo.getMoney() / 100));
        tv_original_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        pb_progress.setMax(shopInfo.getAllstock());
        pb_progress.setProgress(shopInfo.getSellstock());
        double f = new BigDecimal((float) shopInfo.getSellstock() / shopInfo.getAllstock()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        tv_sell.setText("已售" + (f * 100) + "%");

        if (shopInfo.getAllstock() - shopInfo.getSellstock() == 0)
        {
            btn_buy.setEnabled(false);
            btn_buy.setText("已售罄");
        }

        if (!DateUtils.isBefor(curSalesInfo.getStarttime()))
        {
            btn_buy.setVisibility(View.VISIBLE);
            btn_buy.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    listener.buy(shopInfo, curSalesInfo.getId());
                }
            });
        } else
        {
            btn_buy.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewCache
    {
        public ImageView iv_icon;
        public TextView tv_name, tv_desc, tv_price, tv_original_price, tv_sell;
        public ProgressBar pb_progress;
        public Button btn_buy;
    }
}
